<template>
    <div class="lobby-wrapper">
      <!-- 背景装饰 -->
      <div class="background-decoration">
        <div class="color-blob blob-1"></div>
        <div class="color-blob blob-2"></div>
        <div class="color-blob blob-3"></div>
        <div class="grid-overlay"></div>
      </div>
      
      <!-- 主卡片 -->
      <div class="lobby fade-in">
        <!-- 游戏标题和描述 -->
        <div class="lobby-header">
          <div class="logo-container">
            <div class="logo">🎨</div>
          </div>
          <h1 class="lobby-title">涂色抢地盘游戏</h1>
          <p class="lobby-subtitle">与好友一起创作，争夺最大领地！</p>
        </div>
        
        <!-- 游戏特性 -->
        <div class="game-features">
          <div class="feature-item">
            <span class="feature-icon">👥</span>
            <span class="feature-text">多人协作</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">🎯</span>
            <span class="feature-text">策略竞争</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">⏱️</span>
            <span class="feature-text">限时挑战</span>
          </div>
        </div>
        
        <!-- 表单 -->
        <div class="lobby-form">
          <div class="form-group">
            <label class="form-label">创建房间</label>
            <input v-model="roomName" placeholder="输入房间名称" class="lobby-input" />
            <input v-model="customRoomId" placeholder="可选：自定义房间ID" class="lobby-input" />
            <button @click="create" class="lobby-button primary">
              <span class="button-icon">➕</span>
              创建房间
            </button>
            
            <!-- 创建成功后显示房间ID -->
            <div v-if="createdRoomId" class="room-id-display">
              <div class="room-id-label">房间ID：</div>
              <div class="room-id-value">{{ createdRoomId }}</div>
              <button @click="copyRoomId" class="copy-button">
                📋 复制
              </button>
            </div>
          </div>
          
          <div class="divider">
            <span>或</span>
          </div>
          
          <div class="form-group">
            <label class="form-label">加入房间</label>
            <input v-model="roomIdInput" placeholder="输入房间ID" class="lobby-input" />
            <button @click="join" class="lobby-button secondary">
              <span class="button-icon">🚪</span>
              加入房间
            </button>
          </div>
        </div>
        
        <!-- 错误信息显示 -->
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        
        <!-- 页脚 -->
        <div class="lobby-footer">
          <p class="footer-text">🎮 开始你的创意争夺战！</p>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue'
  import { useRoomStore } from '@/stores/room'
  import { socketService } from '@/services/socketService'
  const roomStore = useRoomStore()
  
  // 组件挂载时初始化连接
  onMounted(() => {
    console.log('RoomLobby组件挂载，初始化Socket连接...')
    socketService.initialize()
  })
  const roomName = ref('')
  const customRoomId = ref('')
  const roomIdInput = ref('')
  const errorMessage = ref('')
  const createdRoomId = ref('')
  
  const create = () => {
    // 清除之前的错误信息
    errorMessage.value = ''
    createdRoomId.value = ''
    
    if (!roomName.value.trim()) {
      errorMessage.value = '请输入房间名称'
      return
    }
    
    console.log('创建房间:', roomName.value, '自定义ID:', customRoomId.value || '无')
    
    // 调用roomStore创建房间，它会自动处理路由跳转
    roomStore.createRoom(roomName.value, customRoomId.value || null)
      .then(() => {
        console.log('创建房间成功，即将跳转')
        
        // 清空输入框
        roomName.value = ''
        customRoomId.value = ''
      })
      .catch(err => {
        console.error('创建房间失败:', err)
        errorMessage.value = '无法创建房间: ' + (err.message || '未知错误')
      })
  }
  
  // 复制房间ID到剪贴板
  const copyRoomId = async () => {
    if (createdRoomId.value) {
      try {
        await navigator.clipboard.writeText(createdRoomId.value)
        alert('房间ID已复制到剪贴板！')
      } catch (err) {
        console.error('复制失败:', err)
        errorMessage.value = '复制房间ID失败'
      }
    }
  }
  
  const join = () => {
    // 清除之前的错误信息
    errorMessage.value = ''
    
    if (!roomIdInput.value.trim()) {
      errorMessage.value = '请输入房间ID'
      return
    }
    
    console.log('加入房间:', roomIdInput.value)
    roomStore.joinRoom(roomIdInput.value).catch(err => {
      console.error('加入房间失败:', err)
      errorMessage.value = '无法加入房间: ' + (err.message || '未知错误')
    })
  }
  </script>
  <style scoped>
  /* 全局容器 */
  .lobby-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--bg);
    background-image: var(--bg-gradient);
    position: relative;
    padding: 20px;
  }
  
  /* 背景装饰 */
  .background-decoration {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    z-index: 0;
  }
  
  .color-blob {
    position: absolute;
    border-radius: 50%;
    filter: blur(100px);
    opacity: 0.3;
  }
  
  .blob-1 {
    top: -10%;
    left: -10%;
    width: 500px;
    height: 500px;
    background: var(--primary);
    animation: float 15s ease-in-out infinite;
  }
  
  .blob-2 {
    bottom: -10%;
    right: -10%;
    width: 600px;
    height: 600px;
    background: var(--secondary);
    animation: float 20s ease-in-out infinite reverse;
  }
  
  .blob-3 {
    top: 40%;
    right: 20%;
    width: 300px;
    height: 300px;
    background: var(--accent);
    animation: float 18s ease-in-out infinite;
  }
  
  .grid-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: 
      linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
      linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
    background-size: 40px 40px;
    pointer-events: none;
  }
  
  /* 主卡片 */
  .lobby {
    max-width: 420px;
    width: 100%;
    background: var(--surface);
    background-image: linear-gradient(135deg, var(--surface) 0%, var(--surface-hover) 100%);
    padding: 40px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg), 0 0 40px rgba(0, 0, 0, 0.15);
    border: 1px solid var(--border);
    position: relative;
    overflow: hidden;
    z-index: 1;
    backdrop-filter: blur(10px);
    transition: all 0.3s ease;
  }
  
  .lobby:hover {
    transform: translateY(-5px);
    box-shadow: var(--shadow-lg), 0 15px 40px rgba(0, 0, 0, 0.2);
  }
  
  /* 卡片装饰 */
  .lobby::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: linear-gradient(45deg, transparent 48%, var(--primary-light) 49%, var(--primary-light) 51%, transparent 52%);
    background-size: 30px 30px;
    opacity: 0.05;
    pointer-events: none;
    animation: gridMove 20s linear infinite;
  }
  
  /* Logo区域 */
  .logo-container {
    display: flex;
    justify-content: center;
    margin-bottom: 16px;
  }
  
  .logo {
    font-size: 48px;
    animation: bounce 2s ease-in-out infinite;
  }
  
  /* 标题区域 */
  .lobby-header {
    text-align: center;
    margin-bottom: 32px;
  }
  
  .lobby-title {
    font-size: 32px;
    font-weight: bold;
    margin-bottom: 12px;
    background: linear-gradient(45deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    position: relative;
    display: inline-block;
  }
  
  .lobby-title::after {
    content: '';
    position: absolute;
    bottom: -4px;
    left: 10%;
    width: 80%;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--primary), transparent);
  }
  
  .lobby-subtitle {
    color: var(--text-muted);
    font-size: 16px;
    line-height: 1.4;
  }
  
  /* 游戏特性 */
  .game-features {
    display: flex;
    justify-content: space-around;
    margin-bottom: 32px;
    padding: 20px 0;
    background: rgba(255, 255, 255, 0.03);
    border-radius: var(--radius);
  }
  
  .feature-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
  }
  
  .feature-icon {
    font-size: 24px;
    animation: pulse 2s ease-in-out infinite;
  }
  
  .feature-text {
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 500;
  }
  
  /* 表单区域 */
  .lobby-form {
    display: flex;
    flex-direction: column;
    gap: 24px;
  }
  
  .form-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }
  
  /* 房间ID显示样式 */
  .room-id-display {
    margin-top: 16px;
    padding: 16px;
    background: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.3);
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
  }
  
  .room-id-label {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
  }
  
  .room-id-value {
    flex: 1;
    font-size: 16px;
    font-family: 'Courier New', monospace;
    background: rgba(255, 255, 255, 0.05);
    padding: 8px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    word-break: break-all;
  }
  
  .copy-button {
    padding: 8px 16px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 14px;
    transition: var(--transition);
  }
  
  .copy-button:hover {
    background: var(--primary-dark);
    transform: translateY(-1px);
  }
  
  .form-label {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 4px;
  }
  
  .lobby-input {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    font-size: 16px;
    color: var(--text-primary);
    transition: var(--transition);
    backdrop-filter: blur(5px);
  }
  
  .lobby-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
    background: rgba(255, 255, 255, 0.08);
    transform: translateY(-1px);
  }
  
  .lobby-input::placeholder {
    color: var(--text-muted);
    opacity: 0.7;
  }
  
  /* 按钮样式 */
  .lobby-button {
    padding: 14px 20px;
    font-size: 16px;
    font-weight: 600;
    border-radius: var(--radius);
    border: none;
    cursor: pointer;
    transition: var(--transition);
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }
  
  .lobby-button::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    transform: translate(-50%, -50%);
    transition: width 0.4s, height 0.4s;
  }
  
  .lobby-button:hover::before {
    width: 300px;
    height: 300px;
  }
  
  .lobby-button.primary {
    background: var(--primary);
    color: white;
    border: 2px solid transparent;
  }
  
  .lobby-button.primary:hover {
    background: var(--primary-dark);
    transform: translateY(-2px);
    box-shadow: 0 10px 25px -5px rgba(59, 130, 246, 0.4);
    border-color: var(--primary-light);
  }
  
  .lobby-button.secondary {
    background: transparent;
    color: var(--primary);
    border: 2px solid var(--primary);
  }
  
  .lobby-button.secondary:hover {
    background: var(--primary);
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 10px 25px -5px rgba(59, 130, 246, 0.4);
  }
  
  .button-icon {
    font-size: 18px;
  }
  
  /* 分隔线 */
  .divider {
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--text-muted);
  }
  
  .divider::before,
  .divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
    position: relative;
  }
  
  .divider::before::after,
  .divider::after::before {
    content: '';
    position: absolute;
    top: 50%;
    width: 10px;
    height: 1px;
    background: var(--primary);
  }
  
  .divider::before::after {
    right: 0;
  }
  
  .divider::after::before {
    left: 0;
  }
  
  /* 错误信息 */
  .error-message {
    margin-top: 16px;
    padding: 12px 16px;
    background-color: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: var(--radius);
    color: var(--destructive);
    font-size: 14px;
    text-align: center;
  }
  
  /* 页脚 */
  .lobby-footer {
    text-align: center;
    margin-top: 32px;
    padding-top: 20px;
    border-top: 1px solid var(--border);
  }
  
  .footer-text {
    font-size: 14px;
    color: var(--text-muted);
    font-style: italic;
  }
  
  /* 动画效果 */
  @keyframes float {
    0%, 100% {
      transform: translate(0, 0);
    }
    50% {
      transform: translate(20px, 20px);
    }
  }
  
  @keyframes bounce {
    0%, 100% {
      transform: translateY(0);
    }
    50% {
      transform: translateY(-10px);
    }
  }
  
  @keyframes pulse {
    0%, 100% {
      transform: scale(1);
    }
    50% {
      transform: scale(1.1);
    }
  }
  
  @keyframes gridMove {
    0% {
      transform: translate(0, 0);
    }
    100% {
      transform: translate(30px, 30px);
    }
  }
  
  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(20px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
  
  .fade-in {
    animation: fadeIn 0.8s ease-out;
  }
  
  /* 响应式设计 */
  @media (max-width: 768px) {
    .lobby {
      margin: 5vh auto;
      padding: 32px 24px;
    }
    
    .lobby-title {
      font-size: 28px;
    }
  }
  
  @media (max-width: 480px) {
    .lobby {
      margin: 2vh auto;
      padding: 24px 20px;
      max-width: 95%;
    }
    
    .lobby-title {
      font-size: 24px;
    }
    
    .logo {
      font-size: 40px;
    }
    
    .game-features {
      flex-wrap: wrap;
      gap: 16px;
    }
    
    .feature-item {
      flex: 1 1 30%;
    }
  }
  </style>